package com.tz.farmer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.model.*;
import com.tz.farmer.so.GoodsSo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface GoodsInfoMapper  extends BaseMapper<GoodsInfo> {
    int deleteByPrimaryKey(Integer id);

    int insert(GoodsInfo record);

    int insertSelective(GoodsInfo record);

    GoodsInfo selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(GoodsInfo record);

    int updateByPrimaryKeyWithBLOBs(GoodsInfo record);

    int updateByPrimaryKey(GoodsInfo record);


    @Select("select id, goods_name,goods_photo  from goods_info where goods_id=#{goodsId}" )
    List<GoodsSo> selectGoodsInfos(Integer goodsId);

    @Select("select * from main_body_info where unisc_id=#{uniscId}" )
    MainBodyInfo selectMainBodyInfo(String uniscId);

    @Select("select count(1) from business_info where property='331002' and is_delete='0' " )
    Integer selectMainCount();

    @Select("select count(1) from business_info where  is_delete='0' " )
    Integer selectMain();

    @Select("select count(1) from business_info where unisc_id in (select distinct certificate_code from goods_info) " )
    Integer selectUpBusinessCount();

    @Select("select count(1) from product_info where  lead_id is not null " )
    Integer selectProductCount();

    @Select("select count(distinct batch_info) from goods_info " )
    Integer selectBatch();

    @Select("select count(distinct goods_id) from goods_info " )
    Integer selectUpGoodsCount();

    @Select("select count(1) from business_info where property='331001' and is_delete='0' " )
    Integer selectFishMainCount();

    @Select("select count(1) from visit_records where property=0 " )
    Integer selectVisitCount();

    @Select("select count(1) from visit_records " )
    Integer selectScanCount();

    @Select("select a.p_name as pName,b.o_type as oType,b.o_time as oTime,b.in_thing as inThing from product_info a ,operate_info b where a.id =b.p_info_id and a.p_name='大黄鱼' " )
    List<Map<String,Object>> selectScreenOperateList();

    @Select("select count(1) from check_records " )
    Integer selectCheckCount();

    @Select("select count(1) from visit_records where property=1 " )
    Integer selectFishVisitCount();

    @Select("select count(distinct certificate_code) from goods_info where certificate_code in (select distinct unisc_id from business_info where property='331002')  " )
    Integer selectBatchCount();

    @Select("select count(distinct certificate_code) from goods_info where certificate_code in (select distinct unisc_id from business_info where property='331001')  " )
    Integer selectFishBatchCount();

    @Select("select count(1) from farmer_knowledge " )
    Integer selectKnowledgeCount();

    @Select("select count(1) from farmer_nutrition " )
    Integer selectNutritionCount();

    @Select("select count(1) from farmer_correct " )
    Integer selectCorrectCount();

    @Select("select count(1) from farmer_actives " )
    Integer selectActivityCount();

    @Select("select count(distinct certificate_code) from goods_info  " )
    Integer selectGoodsCount();

    @Select("select * from supervision_info " )
    SupervisionInfo selectSupervisionInfo();

    @Select("select * from fishing_boat " )
    FishingBoat selectFishInfo();

    @Select("select city ," +
            "            SUM(IF(`property`='331001',total,0)) as 'fish'," +
            "            SUM(IF(`property`='331002',total,0)) as 'farmer'" +
            "             from (select city,property,count(1) as total from business_info where is_delete='0'  and city is not null  group by city,property) a group by a.city " )
    List<Map<String,Object>> selectMainTotal();


    @Select("<script>" +
            " select a.month,count(1) as total from (select  DATE_FORMAT(update_time,'%m') as month from business_info" +
            " where property=#{sign} and update_time is not null and city is not null and is_delete ='0' and DATE_FORMAT(update_time,'%Y')=DATE_FORMAT(now(),'%Y')" +
            " <if test = \"city != null and city.trim() !='' \" > " +
            " and city = #{city} " +
            " </if> " +
            " ) a group by a.month "+
            "</script>")

    List<Map<String,Object>> getFarmerTrend(@Param("city") String city,@Param("sign") String sign);


    /**
     * kai 查goods大类名字
     * @return
     */
    @Select("select big_class_code as value ,big_class_name as label from big_class_info " )
    List<BigClassInfoVo> selectBigGoods();
    /**
     * kai 查goods小类名字
     * @return
     */
    @Select("select  small_class_code as value ,small_class_name as label from small_class_info  where big_class_code = #{bigId}" )
    List<SmallClassInfoVo> selectSmallGoods(@Param("bigId")String bigId);
    /**
     * kai 查goods小小类名字
     * @return
     */
    @Select("select  goods_code as value ,goods_name as label from goods_select_info  where small_class_code = #{smallId}" )
    List<GoodsSelectInfoVo> selectGoodsSelect(@Param("smallId")String smallId);

    /**
     * kai 查goods名称
     * @return
     */
    @Select("select DISTINCT(goods_name)  from  goods_info where goods_id = #{goodsId} " )
    GoodsInfo selectGoodsName(String goodsId);


    /**
     * kai 查goods名称
     * @return
     */
    @Select(" SELECT\n" +
            "    id,\n" +
            "    goods_name,\n" +
            "    goods_place,\n" +
            "    produce_time,\n" +
            "    certificate_code,\n" +
            "    inspection_report,\n" +
            "    specs,\n" +
            "    brand,\n" +
            "    update_time,\n" +
            "    business_id,\n" +
            "    amount,\n" +
            "    result,\n" +
            "    goods_photo,\n" +
            "    goods_id,\n" +
            "    trade_time,\n" +
            "    property,\n" +
            "    batch_info\n" +
            "            FROM\n" +
            "    goods_info" +
            "    where " +
            "    goods_id = #{goodsId} " +
            "    and " +
            "    batch_info = #{batchInfo} " )
    GoodsInfo selectOneGoods(@Param("goodsId") Integer goodsId,@Param("batchInfo") String batchInfo);


    @Select("<script>" +
            "select * from goods_info where property = '331001' and goods_id =#{goodsId}  and   batch_info=#{batchInfo}" +
            "</script>")
    GoodsInfo selectBoatDetail(@Param("goodsId") Integer goodsId,@Param("batchInfo") String batchInfo);
}